package com.briup.jd2407cms.utils;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.briup.jd2407cms.domain.Category;
import com.briup.jd2407cms.exception.ServiceException;
import com.briup.jd2407cms.result.Result;
import com.briup.jd2407cms.result.ResultCode;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @BelongsProject: cms2
 * @BelongsPackage: com.briup.jd2407cms.utils
 * @Author: wangjie
 * @CreateTime: 2024-11-22  16:24
 * @Description: TODO
 * @Version: 1.0
 */
public class CategoryParentIdConverter implements Converter<Integer> {
    public static List<Category> list;

    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String value = cellData.getStringValue();
        return convertStringToInteger(value);
    }

    @Override
    public Integer convertToJavaData(ReadConverterContext<?> context) throws Exception {
        String value = context.getReadCellData().getStringValue();
        return convertStringToInteger(value);
    }
    private Integer convertStringToInteger(String value){
        if (value!=null){
            List<String> names = list.stream().map(Category::getName).collect(Collectors.toList());
            if (!names.contains(value)){
                throw new ServiceException(ResultCode.PCATEGORY_IS_INVALID);
            }
            Category category = null;
            for (Category c:list){
                if (value.equals(c.getName())){
                    category=c;
                    break;
                }
            }
            return list.get(list.indexOf(category)).getId();
        }
        return null;
    }

    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return convertIntegerToString(value);
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) throws Exception {
        Integer value = context.getValue();
        return convertIntegerToString(value);
    }
    private WriteCellData<?> convertIntegerToString(Integer value){
        List<Category> categoryList = list.stream().filter(category -> Objects.equals(category.getId(), value)).collect(Collectors.toList());
        if (!categoryList.isEmpty()){
            return new WriteCellData<>(categoryList.get(0).getName());
        }
        return new WriteCellData<>();
    }
}
